<template>
  <div >
    <el-button type="primary" @click="clickAddMaterial">添加原料</el-button>
    <el-table :data="data.materialTableData" style="width: 100%">

      <el-table-column prop="materialId" label="原料编号" > </el-table-column>
      <el-table-column prop="name" label="原料名称" width="180"> </el-table-column>
      <el-table-column  label="质检证明" width="180">
        <template #default="scope">
          <el-button type="primary" @click="checkCertification(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="produceAddress" label="生产地址" width="180"> </el-table-column>
      <el-table-column prop="note" label="备注" width="180"> </el-table-column>
    </el-table>

    <!-- 原料上链对话框 -->
    <el-dialog
        v-model="data.addMaterialVisible"
        title="原料上链"
    >
      <el-form :model="data.addMaterialForm">
        <el-form-item label="原料ID" :label-width="data.formLabelWidth">
          <el-input v-model="data.addMaterialForm.materialId" disabled></el-input>
        </el-form-item>
        <el-form-item label="原料名称" :label-width="data.formLabelWidth">
          <el-input v-model="data.addMaterialForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="生产时间" :label-width="data.formLabelWidth">
          <el-input v-model="data.addMaterialForm.time" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="生产地址" :label-width="data.formLabelWidth">
          <el-input v-model="data.addMaterialForm.produceAddress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="data.formLabelWidth">
          <el-input v-model="data.addMaterialForm.note" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="质检证明" :label-width="data.formLabelWidth">
          <UploadPic @getUploadHash="getUploadHash"></UploadPic>
        </el-form-item>

      </el-form>
      <template #footer>
          <span class="dialog-footer">
            <el-button @click="data.addMaterialVisible = false">取消</el-button>
            <el-button type="primary" @click="handleAddMaterial">确认</el-button>
          </span>
      </template>
    </el-dialog>
    <!-- 质检证明对话框 -->
    <el-dialog v-model="data.certificationVisible">
      <img class="certification" :src="data.certificationSrc" />
    </el-dialog>

  </div>

</template>

<script>
import {onMounted, reactive} from "vue";
import UploadPic from "@/components/UploadPic/index";
import {v1 as uuidv1} from "uuid";
import {
  func_addMaterial,
  func_getAddress,
  func_getMaterial,
  func_getUserInfo,
  func_getUserRole
} from "../../../api/etherneum/api";
import {useStore} from "vuex";
import {useRouter} from "vue-router";
import ipfsAPI from "ipfs-api";
import {ElMessage} from "element-plus";
const ipfs = ipfsAPI({ host: "49.234.138.61", port: "5001", protocol: "http" });
export default {
  name: "Material",
  components:{
    UploadPic
  },
  setup(){
    const data=reactive({
      materialTableData:[],
      address:'',
      currentRole:'',

      addMaterialVisible:false,
      addMaterialForm:{
        materialId:'',
        name:'',
        certification:'',
        produceAddress:'',
        note:''
      },
      formLabelWidth:'140px',
      uploadHash:'',

      certificationSrc:'',
      certificationVisible:false,
      loading:false,
    })
    const store=useStore();
    const router=useRouter();
    const clickAddMaterial=()=>{

      if (data.curUserInfo.role!=1&&data.curUserInfo.role!=2){
        ElMessage.error('无权限')
        return
      }
      data.addMaterialVisible=true
      data.addMaterialForm.materialId=uuidv1()
    }

    const handleAddMaterial=()=>{

      if (!data.addMaterialForm.certification){
        ElMessage.error('图片未上传')
        return
      }

      func_addMaterial(data.addMaterialForm)
      data.addMaterialVisible=false
    }

    const getAddress = async () => {
      const address = await func_getAddress();
      if (address) {
        store.commit("app/SET_ADDRESS", address);
        let role = await func_getUserRole(address);
        store.commit("app/SET_ROLE", role);
        console.log("getUser address=" + address + " role=" + role);
        data.address = address;
        data.currentRole = role;
        data.curUserInfo = await func_getUserInfo(data.address);

      }
    };

    const checkCertification=async (row)=>{
      let hash=row.certification
      data.loading=true
      console.log('certification:',row.certification)
      ipfs.get(hash, async (err, result) => {
        console.log('here')
        if (err) throw err;
        let binaryData = [];
        binaryData.push(result[0].content.buffer);
        data.certificationSrc= window.URL.createObjectURL(new Blob(binaryData))
        console.log('src=',data.certificationSrc)
        data.loading=false
      });

      data.certificationVisible=true
    }

    const getUploadHash=(hash)=>{
      data.addMaterialForm.certification=hash
      console.log('addHash:',hash)
    }

    onMounted(()=>{
      getAddress().then(async () => {
        data.materialTableData=await func_getMaterial(data.address)

      });
      ethereum.on("accountsChanged", () => {
        getAddress();
      });
    })

    return{
      data,
      clickAddMaterial,
      handleAddMaterial,
      getUploadHash,
      checkCertification
    }
  }
}
</script>

<style lang="scss" scoped>
.certification{
  width: 800px;
}
</style>